package com.selfdriving.wl.activity;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nineoldandroids.animation.ArgbEvaluator;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.ValueAnimator;
import com.selfdriving.BaseApplication;
import com.selfdriving.R;
import com.selfdriving.activity.AboutMeMyArticleDetails;
import com.selfdriving.activity.RegisterActivity;
import com.selfdriving.bean.MyArticle;
import com.selfdriving.bean.MyArticleDetails;
import com.selfdriving.utils.Constant;
import com.selfdriving.utils.Utils;
import com.selfdriving.view.MarqueeTextView;

public class ActivityNotice implements OnClickListener
{
	/** 当前View **/
	View view;
	BaseApplication application;
	Context context;

	LinearLayout viewFlipperRL;
	/** VF底部点点 **/
	private LinearLayout Lradio;

	MarqueeTextView tvScrollView;

	TextView vfText;

	ImageView ivImg1;
	ImageView ivImg2;
	ImageView ivImg3;
	ImageView ivImg4;

	List<MyArticle> articles = new ArrayList<MyArticle>();

	public ActivityNotice(BaseApplication application, Context context)
	{
		LayoutInflater inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.activity_notice, null);
		this.application = application;
		this.context = context;
		initViews();
		getHotArticle();
	}

	public void initViews()
	{
		/** VF容器 **/
		viewFlipperRL = (LinearLayout) view.findViewById(R.id.vf_layout);
		/** VF底部点点 **/
		Lradio = (LinearLayout) view.findViewById(R.id.radio_layout);
		tvScrollView = (MarqueeTextView) view.findViewById(R.id.tvScrollView);
		vfText = (TextView) view.findViewById(R.id.viewflipper_size);
		ivImg1 = (ImageView) view.findViewById(R.id.ivImg1);
		ivImg1.setOnClickListener(this);
		ivImg2 = (ImageView) view.findViewById(R.id.ivImg2);
		ivImg2.setOnClickListener(this);
		ivImg3 = (ImageView) view.findViewById(R.id.ivImg3);
		ivImg3.setOnClickListener(this);
		ivImg4 = (ImageView) view.findViewById(R.id.ivImg4);
		ivImg4.setOnClickListener(this);
		ValueAnimator colorAnim = ObjectAnimator.ofInt(tvScrollView,
				"TextColor",
				/* Red */0xFFFF8080, /* Blue */0xFF949494);
		colorAnim.setDuration(3000);
		colorAnim.setEvaluator(new ArgbEvaluator());
		colorAnim.setRepeatCount(ValueAnimator.INFINITE);
		colorAnim.setRepeatMode(ValueAnimator.REVERSE);
		colorAnim.start();
	}

	public View getView()
	{
		return view;
	}

	public TextView getViewText()
	{
		return vfText;
	}

	public LinearLayout getViewFlipperRL()
	{
		return viewFlipperRL;
	}

	public LinearLayout getLradio()
	{
		return Lradio;
	}

	public void getRunText()
	{
		AjaxParams params = new AjaxParams();
		params.put("f", "76");
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				try
				{
					JSONArray array = new JSONArray(json);
					StringBuffer stringBuffer = new StringBuffer();
					for (int i = 0; i < array.length(); i++)
					{
						JSONObject object = array.getJSONObject(i);

						String id = object.getString("id");
						String title = object.getString("title");
						String content = object.getString("content");
						String times = object.getString("times");
						stringBuffer.append("\t\t" + content);
					}
					tvScrollView.setText(stringBuffer);
				} catch (JSONException e)
				{
					e.printStackTrace();
					tvScrollView.setText("");
				}
			}
		});
	}

	Handler handler = new Handler()
	{
		public void handleMessage(android.os.Message msg)
		{
			switch (msg.what)
			{
			case 0:
				getHotArticle();
				break;
			}
		};
	};

	public void getHotArticle()
	{
		AjaxParams params = new AjaxParams();
		params.put("f", "75");
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				handler.sendEmptyMessageAtTime(0, 1000);
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				if (!json.contains("empty"))
				{
					try
					{
						MyArticle myArticle;
						JSONArray jsonArray = new JSONArray(json);
						for (int i = 0; i < jsonArray.length(); i++)
						{
							JSONObject object = jsonArray.getJSONObject(i);
							String id = object.getString("id");
							String title = object.getString("title");
							String headImg = object.getString("headImg");
							String uname = object.getString("uname");
							String times = object.getString("times");
							String laud = object.getString("laud");
							String marrow = object.getString("marrow");
							String clicks = object.getString("clicks");
							String review = object.getString("review");
							String img = object.getString("img0");
							myArticle = new MyArticle(id, title, headImg,
									uname, times, laud, marrow, clicks, review,
									img);
							switch (i)
							{
							case 0:
								application.imageLoader.displayImage(img,
										ivImg1, application.options);
								break;

							case 1:
								application.imageLoader.displayImage(img,
										ivImg2, application.options);
								break;
							case 2:
								application.imageLoader.displayImage(img,
										ivImg3, application.options);
								break;
							case 3:
								application.imageLoader.displayImage(img,
										ivImg4, application.options);
								break;
							}
							articles.add(myArticle);
						}
					} catch (JSONException e)
					{
						handler.sendEmptyMessageAtTime(0, 1000);
					}
				} else
				{
					handler.sendEmptyMessageAtTime(0, 1000);
				}
			}
		});

	}

	@Override
	public void onClick(View v)
	{
		if (!articles.isEmpty())
		{
			MyArticle myArticle;
			String id = null;
			switch (v.getId())
			{
			case R.id.ivImg1:
				myArticle = articles.get(0);
				id = myArticle.getId();
				break;
			case R.id.ivImg2:
				myArticle = articles.get(1);
				id = myArticle.getId();
				break;
			case R.id.ivImg3:
				myArticle = articles.get(2);
				id = myArticle.getId();
				break;
			case R.id.ivImg4:
				myArticle = articles.get(3);
				id = myArticle.getId();
				break;
			}
			gotoImage(id);
		}
	}

	public void gotoImage(String id)
	{
		if (application.user == null)
		{
			Intent intent = new Intent(context, RegisterActivity.class);
			context.startActivity(intent);
		} else if (id != null && !id.equals(""))
		{
			post(id);
		}
	}

	public void post(String id)
	{
		Utils.showLoadingDialog(context, "数据加载中...");
		AjaxParams params = new AjaxParams();
		params.put("f", "19");
		params.put("id", id);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				Utils.dismissLoadingDialog();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				try
				{
					JSONObject object = new JSONObject(json);
					String id = object.getString("id");
					String content = object.getString("content");
					String title = object.getString("title");
					String uid = object.getString("uid");
					String uname = object.getString("uname");
					String times = object.getString("times");
					String laud = object.getString("laud");
					String marrow = object.getString("marrow");
					String clicks = object.getString("clicks");
					String img0 = null;
					String img1 = null;
					String img2 = null;
					if (object.has("img0"))
					{
						img0 = object.getString("img0");
					}
					if (object.has("img1"))
					{
						img1 = object.getString("img1");
					}
					if (object.has("img2"))
					{
						img2 = object.getString("img2");
					}
					application.myArticleDetails = new MyArticleDetails(id,
							content, title, uid, uname, times, laud, marrow,
							clicks, img0, img1, img2);
					Utils.dismissLoadingDialog();
					Intent intent = new Intent(context,
							AboutMeMyArticleDetails.class);
					context.startActivity(intent);
				} catch (JSONException e)
				{
					Utils.dismissLoadingDialog();
					e.printStackTrace();
				}
			}
		});
	}
}
